package com.community.util;

import com.community.common.Const;
import com.community.pojo.Answer;
import com.community.pojo.User;
import com.github.pagehelper.PageInfo;
import com.community.common.ServerResponse;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 查询大字段
 */
public class SearchBigFieldUtil {

    private static Logger log = LoggerFactory.getLogger(SearchBigFieldUtil.class);
    private static HttpSolrServer solrServer = SolrUtil.getSolrServer();
    private static int maxSize = 300;


    /**
     * 查询大字段
     * @param mapList
     * @param entityName
     * @param type
     * @return
     */
    public static List<Map> selectBigText(List<Map> mapList, String entityName,
                                          String type, HttpServletRequest request){
        Object o = request.getSession().getAttribute(Const.CURRENT_USER);//获取登录用户
        User u = null;
        if(o != null){
            u=(User) o;
        }
        for (int i = 0; i < mapList.size() ; i++) {
            //根据ID查询对应大字段
            SolrDocumentList list = getByIdAndEntityNameAndType(
                    NumberUtil.safeToNumber(mapList.get(i).get("id"))
                    ,entityName,type);
            if(null != list && 0 < list.size()){
                //判断答案是否为收费查看模式
                if(entityName.equals(Answer.ENTITYNAME) &&
                        NumberUtil.safeToNumber(mapList.get(i)
                                .get("answer_model")) == Const.AnswerModel.PAY){
                    if(u != null){//如果登录用户是答案作者
                        if(u.getUsername().equals(StringUtil.valueOfString(mapList.get(i).get("username")))){
                            mapList.get(i).put("content",
                                    list.get(0).getFieldValue("bigfield"));
                            continue;
                        }
                    }
                    //利用正则去除样式标签
                    String content = DelTagUtil.delHTMLTag(StringUtil
                            .valueOfString(list.get(0).getFieldValue("bigfield")));
                    //判断长度
                    int strLength = content.length();
                    if(strLength > 3){
                        strLength = (int)Math.ceil(strLength/3);//展示三分之一
                        mapList.get(i).put("content",content.substring(0,strLength));
                    }
                }else{
                    mapList.get(i).put("content",
                            list.get(0).getFieldValue("bigfield"));
                }
            }
        }
        return mapList;
    }

    /**
     * 查询对应大字段
     * @return
     */
    public static ServerResponse<PageInfo> selectBigText(PageInfo pageInfo,String entityName,
                                                         String type,HttpServletRequest request){
        List<Map> mapList = pageInfo.getList();
        try {
            if(null != mapList && 0 < mapList.size()){
                pageInfo.setList(selectBigText(mapList,entityName,type,request));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.createBySuccess(pageInfo);
    }

    public static String searchContentById(Integer joinId,String entityName,String type){
        //查询
        SolrQuery params = new SolrQuery("joinid:"+"\""+joinId+"\""
                +"AND entityName:" + "\""+entityName+"\""+"AND type:"+"\""+type+"\"");
        //指定显示域
        String[] fields = {"bigfield"};
        params.setFields(fields);
        QueryResponse response = null;
        try {
            response = solrServer.query(params);
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
        //返回值
        SolrDocumentList list = response.getResults();
        if(null != list && 0 < list.size()){
            return StringUtil.valueOfString(list.get(0).getFieldValue("bigfield"));
        }
        return "";
    }

    public static PageInfo pageInfo(int pageNum,int pageSize,List<Map> mapList){
        ListSub<Map> listSub = new ListSub<Map>(pageNum, pageSize, mapList);
        PageInfo pageInfo = new PageInfo();
        pageInfo.setTotal(listSub.getTotal());
        pageInfo.setPages(listSub.getPages());
        pageInfo.setPageSize(listSub.getPageSize());
        pageInfo.setList(listSub.getList());
        return pageInfo;
    }

    /**
     * 查询单个大文本
     * @param id
     * @param entityName
     * @param type
     * @return
     */
    public static SolrDocumentList getByIdAndEntityNameAndType(Integer id,String entityName,String type){
        //查询
        SolrQuery params = new SolrQuery("joinid:"+id+"AND entityName:" +
                entityName+"AND type:"+type);
        //指定显示域
        String[] fields = {"bigfield"};
        params.setFields(fields);
        QueryResponse response = null;
        try {
            response = solrServer.query(params,SolrRequest.METHOD.POST);
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
        return response.getResults();
    }
}
